<?php

namespace App\Admin\Controllers;

use App\Models\ShopCom;
use App\Admin\Forms\Addshopcom;
use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;




class ShopComController extends AdminController
{

    protected $title = '店铺商品';

    public function index(Content $content)
    {
        return $content
            ->title('添加店铺商品')
            ->body(new Addshopcom());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ShopCom);

        $grid->column('id', __('Id'));
        $grid->column('shop_id', __('Shop id'));
        $grid->column('commodity_id', __('Commodity id'));
        $grid->column('commodity_attr_id', __('Commodity attr id'));
        $grid->column('stock', __('Stock'));
        $grid->column('price', __('Price'));
        $grid->column('is_buy', __('Is buy'));
        $grid->column('status', __('Status'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ShopCom::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('shop_id', __('Shop id'));
        $show->field('commodity_id', __('Commodity id'));
        $show->field('commodity_attr_id', __('Commodity attr id'));
        $show->field('stock', __('Stock'));
        $show->field('price', __('Price'));
        $show->field('is_buy', __('Is buy'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        $form = new Form(new ShopCom);

        $form->number('stock', __('库存'))->default(0);
        $form->decimal('price', __('换购价格'))->default(0.00);
        $form->radio('is_buy', __('能否换购'))->options([1 => '是', 0 => '否'])->default(1);
        $form->radio('status', __('是否上架'))->options([1 => '是', 0 => '否'])->default(1);
       
        $form->saved(function (Form $form) {
            admin_success('修改成功.');         
            return redirect('/admin/shopdetail/'.$form->model()->shop_id);
        });
        return $form;

    }

    public function delcom(Request $request)
    {
        $id = $request->id;
        $res = ShopCom::where('id',$id)->delete();
        if($res){
            admin_success('删除店铺商品','成功.');
            return redirect()->back();
        }else{
            admin_error('删除店铺商品','失败.');
            return redirect()->back();
        }
    }
    
}
